package com.jwell.panprovider.ui.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.jwell.cargoapp.ui.base.MVPBaseActivity
import com.jwell.panprovider.R
import com.jwell.panprovider.application.App
import com.jwell.panprovider.contract.WaybillContract
import com.jwell.panprovider.entity.ForExportWaybillListDto
import com.jwell.panprovider.model.WaybillModel
import com.jwell.panprovider.presenter.WaybillPresenter
import com.jwell.panprovider.ui.adapter.WaybillAdapter
import com.jwell.panprovider.ui.custom.DateChooseWheelViewDialog
import kotlinx.android.synthetic.main.activity_waybill_search.*
import kotlinx.android.synthetic.main.empty_layout.*
import kotlinx.android.synthetic.main.error_layout.*
import kotlinx.android.synthetic.main.list_layout.*
import kotlinx.android.synthetic.main.toolbar.*

/**
 * 运单搜索界面
 */
class WaybillSearchActivity : MVPBaseActivity<WaybillPresenter>(), WaybillContract.View, View.OnClickListener {


    private var haveSearch = false
    private var searchData = WaybillModel.RequestData()
    private var planId = 0
    private var listData = ArrayList<ForExportWaybillListDto>()
    private var adapter = WaybillAdapter(listData)

    override fun createPresenter(): WaybillPresenter? {
        return WaybillPresenter()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_waybill_search)
        initToolBar("运单搜索", true, null)
        toolbar_right_text_save.text = "搜索"
        toolbar_right_text_save.visibility = View.VISIBLE
        setListener()
        initData()
    }

    private fun setListener() {
        text_time1.setOnClickListener(this)
        text_time2.setOnClickListener(this)
        toolbar_right_text_save.setOnClickListener(this)
        rl_item11.setOnClickListener(this)
        rl_item12.setOnClickListener(this)
        rl_item13.setOnClickListener(this)
        rl_item14.setOnClickListener(this)
        rl_item15.setOnClickListener(this)
        rl_item16.setOnClickListener(this)


    }

    private fun setLabel(c1: Boolean, c2: Boolean, c3: Boolean, c4: Boolean, c5: Boolean, c6: Boolean) {
        rl_item11.isSelected = c1
        rl_item12.isSelected = c2
        rl_item13.isSelected = c3
        rl_item14.isSelected = c4
        rl_item15.isSelected = c5
        rl_item16.isSelected = c6
        setTextColor(c1, text_label11, image_tick_11)
        setTextColor(c2, text_label12, image_tick_12)
        setTextColor(c3, text_label13, image_tick_13)
        setTextColor(c4, text_label14, image_tick_14)
        setTextColor(c5, text_label15, image_tick_15)
        setTextColor(c6, text_label16, image_tick_16)
    }

    private fun setTextColor(isChecked: Boolean, textView: TextView, imageView: ImageView) {
        when (isChecked) {
            true -> {
                textView.setTextColor(application.resources.getColor(R.color.black))
                imageView.visibility = View.VISIBLE
            }
            false -> {
                textView.setTextColor(application.resources.getColor(R.color.c999999))
                imageView.visibility = View.GONE
            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.text_time1 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time1.text = time
                })
                startDateChooseDialog.setDateDialogTitle("供应开始时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.text_time2 -> {
                val startDateChooseDialog = DateChooseWheelViewDialog(this@WaybillSearchActivity, DateChooseWheelViewDialog.DateChooseInterface { time, longTimeChecked ->
                    text_time2.text = time
                })
                startDateChooseDialog.setDateDialogTitle("供应结束时间")
                startDateChooseDialog.setTimePickerGone(true)
                startDateChooseDialog.showDateChooseDialog()
            }
            R.id.toolbar_right_text_save -> {
                when (toolbar_right_text_save.text) {
                    "重新搜索" -> {
                        list_layout.visibility = View.GONE
                        scrollView_search.visibility = View.VISIBLE
                        haveSearch = false
                    }
                    "搜索" -> {
                        if (rl_item11.isSelected) {
                            searchData.billState = 1
                        }
                        if (rl_item12.isSelected) {
                            searchData.billState = 2
                        }
                        if (rl_item13.isSelected) {
                            searchData.billState = 3
                        }
                        if (rl_item14.isSelected) {
                            searchData.billState = 4
                        }
                        if (rl_item15.isSelected) {
                            searchData.billState = 5
                        }
                        if (rl_item16.isSelected) {
                            searchData.billState = 9
                        }
//                        if (rl_item17.isSelected) {
//                            searchData.billState = 8
//                        }
//                        if (rl_item18.isSelected) {
//                            searchData.billState = 8
//                        }
//                        if (rl_item19.isSelected) {
//                            searchData.billState = 8
//                        }
                        searchData.filter = edit_filter.text.toString()
                        searchData.stateDate = text_time1.text.toString()
                        searchData.endDate = text_time2.text.toString()
                        searchData.skipCount = 0
                        searchData.planId = planId
                        showProgress("", true)
                        presenter?.getListData(searchData)
                        haveSearch = true
                        list_layout.visibility = View.VISIBLE
                        scrollView_search.visibility = View.GONE
                    }
                }
                when (haveSearch) {
                    true -> {
                        toolbar_right_text_save.text = "重新搜索"
                    }
                    false -> {
                        toolbar_right_text_save.text = "搜索"
                    }
                }
            }
            R.id.rl_item11 -> setLabel(true, false, false, false, false, false)
            R.id.rl_item12 -> setLabel(false, true, false, false, false, false)
            R.id.rl_item13 -> setLabel(false, false, true, false, false, false)
            R.id.rl_item14 -> setLabel(false, false, false, true, false, false)
            R.id.rl_item15 -> setLabel(false, false, false, false, true, false)
            R.id.rl_item16 -> setLabel(false, false, false, false, false, true)
        }
    }

    private fun initData() {
        intent?.let {
            planId = it.getIntExtra("id", 0)
        }
        initEmptyAndErrorView()
        loadingLayout.visibility = View.GONE
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        freshLayout.setEnableLoadmoreWhenContentNotFull(true)
        freshLayout.setEnableFooterFollowWhenLoadFinished(true)
        freshLayout.setOnRefreshListener {
            presenter?.fresh()
        }
        freshLayout.setOnLoadmoreListener {
            presenter?.load()
        }
    }

    private fun initEmptyAndErrorView() {
        image_empty.setImageResource(R.mipmap.icon_waybill)
        text_empty_describe.text = "无搜索到符合条件的运单"
        ll_error.setOnClickListener {
            errorLayout.visibility = View.GONE
            loadingLayout.visibility = View.VISIBLE
            presenter?.fresh()
        }
    }

    override fun showListData(data: List<ForExportWaybillListDto>, isFresh: Boolean) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            listData.clear()
            freshLayout.finishRefresh()
            //设置可以加载更多
            freshLayout.resetNoMoreData()
            if (App.DEBUG) println("设置可以加载更多")
        } else {
            freshLayout.finishLoadmore()
        }
        listData.addAll(data)
        adapter.notifyDataSetChanged()
        if (listData.size == 0) {
            emptyLayout.visibility = View.VISIBLE
        } else if (data.size < 10) {
            freshLayout.finishLoadmoreWithNoMoreData()
            if (App.DEBUG) println("设置不可以加载更多")
        }
    }

    override fun showListDataFailed(isFresh: Boolean, str: String) {
        loadingLayout.visibility = View.GONE
        errorLayout.visibility = View.GONE
        emptyLayout.visibility = View.GONE
        if (isFresh) {
            freshLayout.finishRefresh(false)
            if (listData.size == 0) errorLayout.visibility = View.VISIBLE
            freshLayout.finishLoadmore(false)
        } else {
            freshLayout.finishLoadmore(false)
        }
    }
}
